﻿using Microsoft.AspNetCore.Mvc;
using SIMS.Shared.DTO;
using SIMS.WebApi.Models;
using SIMS.WebApi.Services.Student;

namespace SIMS.WebApi.Controllers
{
    /// <summary>
    /// 学生控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class StudentController : ControllerBase
    {
        private readonly ILogger<StudentController> logger;

        private readonly IStudentAppService _service;

        public StudentController(ILogger<StudentController> logger, IStudentAppService service) {
            this.logger = logger;
            this._service = service;
        }

        /// <summary>
        /// 学生列表，带分页
        /// </summary>
        /// <param name="pageNum"></param>
        /// <param name="pageSize"></param>
        /// <param name="no"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetStudents(int pageNum, int pageSize, string? no = null, string? name = null ) 
        {
            var data = _service.GetStudents(no, name, pageNum, pageSize);

            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }

        /// <summary>
        /// 查询某一班级的学生列表
        /// </summary>
        /// <param name="classId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetStudentsByClasses(int classId) 
        {
            var data = _service.GetStudentsByClasses(classId);

            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }

        /// <summary>
        /// 获取学生信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetStudent(int id) 
        {
            var data = _service.GetSudent(id);

            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }

        /// <summary>
        /// 新增学生
        /// </summary>
        /// <param name="student"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddStudent(StudentDTO student)
        {
            var data = _service.AddStudent(student);

            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }

        /// <summary>
        /// 修改学生
        /// </summary>
        /// <param name="student"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateStudent(StudentDTO student)
        {
            var data = _service.UpdateStudent(student);

            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }

        /// <summary>
        /// 删除学生
        /// </summary>
        /// <param name="id"></param>
        [HttpDelete]
        public IActionResult DeleteStudent(int id)
        {
            var data = _service.DeleteStudent(id);

            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }
    }
}
